<?php
/**
 * Created by PhpStorm.
 * User: Lenovo
 * Date: 2019/4/26
 * Time: 15:30
 */

namespace app\admin\model;
use think\Session;

class Qrcode extends BaseModel
{
    protected $table='jc_qrcode';

   public function qrcodeindex()
    {
        //获取当前登陆的id
        $uid = Session::get('admin_uid');
        $vip = Guanli::where('id',$uid)->find();
        $arr = User::where('uid',$vip['uid'])->find();
        $rs=$this->alias('qr')
            ->join('jc_user us','us.uid=qr.uid','left')
            ->join('jc_zpaytype zt','zt.zid=qr.zid','left')
            ->order('qr.qid desc')
            ->paginate(7);
        if($arr['daili']==1)
        {
            $rs=$this->alias('qr')
                ->join('jc_user us','us.uid=qr.uid','left')
                ->join('jc_zpaytype zt','zt.zid=qr.zid','left')
                ->where('us.u_path','like','%,'.$arr['uid'].',%')
                ->order('qr.qid desc')
                ->paginate(7);
        }
        if(input('sub')) {
            $where = array();
            $cateid = input('cateid');//下拉菜单
            $key = input('Key');//输入框
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
            if (isset($datemin) && $datemin != '') {
                $where['q_createtime'] = ['>=', $datemin . ' 00:00:00'];
                //dump($where);
            }
            if (isset($datemax) && $datemax != '') {
                $where['q_createtime'] = ['<=', $datemax . ' 23:59:59'];
            }

            if ($cateid == 'u_phone') {
                $where['u_phone'] = ['like', '%' . $key . '%'];
            } else if ($cateid == 'u_name') {
                $where['u_name'] = ['like', '%' . $key . '%'];
            } else if ($cateid == 'jc_qrcode.zid') {
                if ($key == '银行卡') {
                    $key = 3;
                } elseif ($key == '支付宝') {
                    $key = 2;
                } elseif ($key == "微信") {
                    $key = 1;
                    //echo $key;die;
                }
                $where['qr.zid'] = ['like', '%' . $key . '%'];
            }
            $rs=$this->alias('qr')
                ->join('jc_user us','us.uid=qr.uid','left')
                ->join('jc_zpaytype zt','zt.zid=qr.zid','left')
                ->order('qr.qid desc');
           return $rs=$rs->where($where)->paginate(7);
        }
        else
        {
            return $rs;
        }
    }
    //二维码锁定状态的改变
    public function qrstate()
    {
        $qid=input('qid');
        $cha=$this->where('qid',$qid)->find();
        if($cha['q_state']=='0')
        {
            $state=1;
        }
        else
        {
            $state=0;
        }
        $xiu=$this->where('qid',$qid)->update(['q_state'=>$state]);
        if($xiu)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //用户表连接
    public function userQrcde(){
        return $this->belongsTo('User','uid','uid');
    }
    //支付方式
    public function zPayQrcde(){
        return $this->belongsTo('Zpaytype','zid','zid');
    }
    //二维码一级
    public static function getQrcodeA(){
        $data['tid'] = input('tid');
        $data['sid'] = config('stats.successyuyue');
        $yuyue = new Indent();
        $yu = $yuyue->where($data)->find();
        $where['uid']=$yu['uid'];
        $where['zid']=$yu['zid'];
        $qrcode = self::where($where)->select();
        foreach ($qrcode as $va=>$val){
            $a[]=$qrcode[$va];
        }
        return $a;
    }
    //二维码二级
    public static function getQrcodeB(){
        $qid=input('qid');
        return self::where('qid',$qid)->find();
    }
    //锁定
    public static function getSuoding(){
        $qid = input('qid');
        $ding = self::where('qid',$qid)->find();
        if($ding['q_state']==1){
            $qst = config('qstats.q_stats');
        }else{
            $qst = config('qstats.qstats');
        }
        $suo = self::where('qid',$qid)
            ->update(['q_state'=>$qst]);
        if ($suo){
            return 1;
        }else{
            return 0;
        }
    }
}